﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BaseDoc.Model;
using Yipusoft.Account.Model;

public partial class Task_ProgressNew : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            BindOrder();
        }
    }
    private void Bind()
    {
        dptParent.Items.Clear();
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0,4)+"000";
        ProgressInfo pinfo = Yipusoft.BaseDoc.BLL.Progress.GetProgress(DepartmentID);
        if (pinfo != null)
        {
            dptParent.Items.Add(new ListItem(pinfo.FullName, pinfo.DepartmentID));
            SetOrder(DepartmentID);
        }
        else
        {
            dptParent.Items.Insert(0, new ListItem("顶级分类", "00"));
        }
    }

    private void BindOrder()
    {
        dptOrderID.Items.Clear();
        for (int i = 0; i <= 50; i++)
        {
            dptOrderID.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }
    private void SetOrder(string DepartmentID)
    {
        List<ProgressInfo> templist = Yipusoft.BaseDoc.BLL.Progress.GetList(" ParentID='" + DepartmentID + "' Order By OrderID");
        if (templist != null)
        {
            foreach (ProgressInfo info in templist)
            {
                dptParent.Items.Add(new ListItem("".PadLeft(2*info.ItemLevel,'　')+info.FullName,info.DepartmentID));
                SetOrder(info.DepartmentID);
            }
            
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblPrompt.Text = "";
        UserInfo userinfo=Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        ProgressInfo info = new ProgressInfo();
        if (dptParent.SelectedValue.Equals("00"))
        {
            info.DepartmentID = userinfo.DepartmentID.Substring(0, 4) + "000";
            info.ItemLevel = 0;
            info.ParentID = "0";
        }
        else
        {
            info.DepartmentID = (Yipusoft.BaseDoc.BLL.Progress.GetMaxID(userinfo.DepartmentID) + 1).ToString();
            info.ItemLevel = Yipusoft.BaseDoc.BLL.Progress.GetProgress(dptParent.SelectedValue).ItemLevel + 1;
            info.ParentID = dptParent.SelectedValue;
        }
        info.CreateUser = User.Identity.Name;
        info.FullName = txtFullName.Text;
        info.IsPass = cbx.Checked;
        info.CreateDate = DateTime.Now;
        try
        {
            lblPrompt.Text = Yipusoft.BaseDoc.BLL.Progress.AddProgress(info) ? "添加成功" : "添加失败";
        }
        catch
        {
            lblPrompt.Text = "添加失败";
            return;
        }

        Bind();
        dptParent.SelectedValue = info.ParentID;

        
    }
}
